package org.dcits.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.dcits.model.ReturnData;
import org.dcits.model.Useracct;
import org.dcits.service.TransHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author czl
 * 交易历史
 */
@Controller
public class TransHistoryController {
	
	@Autowired
	private TransHistoryService transHistoryService;
	

	/**
	 * 查询当前客户交易历史
	 * @param ClientNo 当前客户  statu  判断转出还是转入
	 * @return
	 * @throws Exception 
	 */
	@GetMapping("/queryTranHistory")
	@ResponseBody
	public ReturnData queryTranHistory(@RequestParam("ClientNo") String ClientNo,@RequestParam("statu")String statu) throws Exception{
		List<Map<String, Object>> queryTranHistory = transHistoryService.queryTranHistory(ClientNo,statu);
		if(queryTranHistory!=null){
			return new ReturnData<>("000000", "查询成功",queryTranHistory);
		}else{
			return new ReturnData<>("888888", "没有交易记录",null);
		}
	}
	
	/**
	 * 跳转交易界面
	 * @return
	 * @throws Exception 
	 */
	@GetMapping("/toTransHistory")
	public String ToTransHistory(HttpSession hs) throws Exception{
		int userId = (int) hs.getAttribute("userId");
		List<Map<String, Object>> accno = transHistoryService.findAccNoByUserId(userId);
		Map<String, Object> map = accno.get(0);
		String acct_no = (String) map.get("acct_no");
		hs.setAttribute("acct_no", acct_no);
		return "TransHistory";
	}
	
	
	
	/**
	 * 功能：转账时客户号是否正确  
	 * 技术：异步刷新
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/queryClientno/{acctNo}")
	@ResponseBody
	public ReturnData<Useracct> returnClientNoInfo(@PathVariable String acctNo) throws Exception{
		
		boolean flag = transHistoryService.isExistClientNo(acctNo);
		
		if(flag==true){
			return new ReturnData<>("000000", "存在此賬號", null);
		}else{
			return new ReturnData<>("999999", "不存在此賬號", null);
		}
	}
	/**
	 * 转账操作
	 * 交易对象：转账方a_tran，收账方b_tran，转账金额tranSalary
	 * @return
	 */
	@PostMapping("/transFlow")
	@ResponseBody
	public ReturnData transSalary(String a_tran,String b_tran,int tranSalary){
		
		
		try {
			boolean aClient = transHistoryService.isExistClientNo(a_tran);
			boolean bClient = transHistoryService.isExistClientNo(b_tran);
			if(aClient==false){
				return new ReturnData<>("999999","转账方账号输入有误",null);
			}else if(bClient==false){
				return new ReturnData<>("999999","收账方账号输入有误",null);
			}else{

			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
		
		
	}
	
}
